<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<form action="" method="">
			<table border="1" cellspacing="0" cellpadding="10" id="tab">
				<tr>
					<th></th>
					<th>姓名</th>
					<th>性别</th>
					<th>年龄</th>
				</tr>
				<tr>
					<td><input type="checkbox"/></td>
					<td>张三</td>
					<td class="sex">男</td>
					<td>18</td>
				</tr>
				<tr>
					<td><input type="checkbox"/></td>
					<td>李四</td>
					<td class="sex">男</td>
					<td>18</td>
				</tr>
				<tr>
					<td><input type="checkbox"/></td>
					<td>王五</td>
					<td class="sex">男</td>
					<td>19</td>
				</tr>
				<tr>
					<td><input type="checkbox"/></td>
					<td>赵六</td>
					<td class="sex">女</td>
					<td>17</td>
				</tr>
				<tr>
					<td><input type="checkbox"/></td>
					<td>钱七</td>
					<td class="sex">女</td>
					<td>18</td>
				</tr>
			</table>
		</form>
		<input type="checkbox" id="ccall" onclick="onall()"/>全选
		<input type="checkbox" id="ccallboy" onclick="onallboy()"/>全选男
		<input type="checkbox" id="ccallgirl" onclick="onallgirl()"/>全选女
	</body>
	<script type="text/javascript">
		function onall(){
			var a = document.getElementById('ccall').checked;
			var b = document.getElementById('tab');
			var c = b.getElementsByTagName('input');
			console.log(a);
			for(var i=0 ; i<c.length ; i++){
				c[i].checked = a;
			}
		}
		function onallboy(){
			var a = document.getElementById('ccallboy').checked;
			var b = document.getElementById('tab');
			var c = b.getElementsByTagName('input');
			var d = b.getElementsByClassName('sex');
			console.log(d);
			for(var i=0 ; i<c.length ; i++){
				if(d[i].innerText == '男'){
					console.log(i);
					console.log(c[i].checked);
					c[i].checked = a;
				}
				else if(d[i].innerText == '女'){
					if(a == true){
						c[i].checked = false;
					}
					else if(a == false){
						d[i].checked = false;
					}
				}
			}
		}
		function onallgirl(){
			var a = document.getElementById('ccallgirl').checked;
			var b = document.getElementById('tab');
			var c = b.getElementsByTagName('input');
			var d = b.getElementsByClassName('sex');
			console.log(d);
			for(var i=0 ; i<c.length ; i++){
				if(d[i].innerText == '女'){
					console.log(i);
					console.log(c[i].checked);
					c[i].checked = a;
				}
				else if(d[i].innerText == '男'){
					if(a == true){
						c[i].checked = false;
					}
					else if(a == false){
						d[i].checked = false;
					}
				}
			}
		}
	</script>
</html>
